In this age of extensive social networking, it is essential for organizations to monitor customer sentiment via solutions that monitor platforms such as twitter. This document provides guidance on how to create integration between a tweet analysis solution, and PagerDuty.
Use case:
Allow PagerDuty to create an incident whenever a tweet contains a predefined keyword and in the PagerDuty Infrastructure Health Console place a "Tâ marker on the graph.
Prereqs:
These procedures assumes you have:
- A PagerDuty account with admin authority
- A twitter account with developer access (needed in order to get required API tokens)
- A small server (virtual or otherwise) with python 2.7 or higher installed
PART 1: PAGERDUTY SETUP:
- Select Configuration/Services then create/modify a service you want to receive the tweet notification on. Select the integration tab the click ânew integrationâ
- Name your integration âTwitter Custom Event Transformerâ, and select from the pulldown menu âcustom event transformerâ, then choose an existing escalation policy. Since this service is meant to be automatic you may wish to create a âno opsâ escalation policy so no one gets notified. You may elect to have the incidents auto resolve after 10 minutes. In our Java script below we also set the severity to âinfoâ so that the alert gets tagged as a âlow priorityâ notification. More information on Custom Event Transformer can be found here: https://developer.pagerduty.com/docs/cet
- Select âedit integrationâ to modify the provided default java script provided. Replace the existing js example with the following code (cut and paste the following segment):
var body = PD.inputRequest.body;
var twitter_description = String(PD.inputRequest.body.description);
var cef_event_deploy = {
event_type: 'cef',
event_action: PD.Trigger,
summary: twitter_description,
description: twitter_description,
message: body.description,
client_url: 'https://twitter.com/',
client: 'Twitter',
source_origin: 'Twitter via Tweepy API',
severity: 'info',
event_class: 'tweet'
};
PD.emitCEFEvents([cef_event_deploy]);
- Save your changes and copy to your clipboard the integration key.
PART 2: TWEEPY SETUP:
- We have tested this approach on two types of servers. One was a local CentOS VM running off of a Macbook pro via the VirtualBox. The other was an AMI instance off of AWS running Ubuntu. You can select whichever system is more appropriate for you.
- Ensure you have a python environment available in order to execute the code below
- Download and install the PagerDuty agent. The PagerDuty agent is needed to send the information over to pagerduty via the âpd-sendâ command line utility. Details and Instructions for installing the pdagent API are located here: https://www.pagerduty.com/docs/guides/agent-install-guide/
- Download and install the tweepy API.
Here are instructions for getting it for a CentOS distro: https://www.mikesel.info/install-tweepy-centos-6-6/ - Modify the tweepy code lines 11,12,13,14 to use the twitter API tokens from your twitter developer account. Also change the âYOUR_PD_API_Keyâ in the code below with your key captured from Part 1, step 4 above. Notice line 15 âkeywordsâ contains the twitter âstringâ to listen for (ie. âpagerdutyâ). Make your modifications and save the file as tweepypd.py
- We now need to start a terminal session and run your modified script. At the command line type:
python ./tweepypd.py
#!/usr/bin/python
# Import the necessary package to process data in JSON format
try:
import json
except ImportError:
import simplejson as json
# Import the tweepy library
import tweepy
import os
# Put keywords that you want to filter into an array.
keywords = ['pagerduty']
# Variables that contains the user credentials to access Twitter API
ACCESS_TOKEN = '<YOUR_TWITTER_API_DEV_TOKENS>'
ACCESS_SECRET = '<YOUR_TWITTER_API_DEV_TOKENS>'
CONSUMER_KEY = '<YOUR_TWITTER_API_DEV_TOKENS>'
CONSUMER_SECRET = '<YOUR_TWITTER_API_DEV_TOKENS>'
# Setup tweepy to authenticate with Twitter credentials:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
# Create the api to connect to twitter with your credentials
api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True, compression = True)
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
status_var = "\"" + status.text + "\""
os.system('pd-send -k <YOUR_PD_API_KEY> -t trigger -d \""' + status_var.encode("utf-8") + '\""')
def on_error(self, status_code):
if status_code == 420:
return False
stream_listener = StreamListener()
stream = tweepy.Stream(auth = api.auth, listener = stream_listener)
stream.filter(track = keywords)
PART 3: GENERATING THE INCIDENT
-
You can wait for a PagerDuty incident to be created when a tweet containing your âkeywordâ shows up or you can login to your twitter account and post a tweet with the âkeywordâ that you would like to trigger on (ie. âpagerdutyâ in our example).
-
The PagerDuty twitter service will receive and surface an incident. It will also create a âtâ marker in the visibility infrastructure health console, indicating a tweet with the âkeywordâ was
captured.
Conclusion:
Disruptive organizations are leveraging sentiment analysis and can use PagerDuty to quickly recover from negative tweets, ensuring customer loyalty and helping reduce churn risk.